package com.lovi.search.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lovi.param.ProductSearchParam;
import com.lovi.pojo.Product;
import com.lovi.search.service.SearchService;
import com.lovi.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @PostMapping("/product")
    public R productList(@RequestBody ProductSearchParam productSearchParam) throws JsonProcessingException {


        return searchService.search(productSearchParam);
    }

    /**
     * 数据同步，商品插入和更新！
     * @param product
     * @return
     * @throws IOException
     */
    @PostMapping("save")
    public R saveProduct(@RequestBody Product product) throws IOException {

        return searchService.save(product);
    }

    /**
     * 数据同步，商品删除！
     * @param productId
     * @return
     * @throws IOException
     */
    @PostMapping("remove")
    public R removeProduct(@RequestBody Integer productId) throws IOException {

        return searchService.remove(productId);
    }

}
